package com.iptech.example.client.widgets;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.TabItemConfig;
import com.sencha.gxt.widget.core.client.TabPanel;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.button.ToggleButton;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;

public class ButtonGroup implements IsWidget, SelectHandler, ValueChangeHandler<Boolean> {

	private HorizontalPanel hp;
	private TextButton addTabButton;
	private ToggleButton enableContextMenuButton;
	private final TabPanel tabpanel;
	private static int index;
	
	public ButtonGroup(TabPanel tabpanel){
		this.tabpanel = tabpanel;
	}
	
	
	@Override
	public Widget asWidget() {
		initWidget();
		return hp;
	}
	
	private void initWidget(){
		hp = new HorizontalPanel();
		hp.setSpacing(10);
		
		addTabButton = new TextButton("add Tab");
		addTabButton.addSelectHandler(this);
		
		enableContextMenuButton = new ToggleButton("enable context menu");
		enableContextMenuButton.setShadow(true);
		enableContextMenuButton.setValue(true);
		enableContextMenuButton.addValueChangeHandler(this);
		
		hp.add(addTabButton);
		//hp.add(enableContextMenuButton);
	}

	@Override
	public void onSelect(SelectEvent event) {
		addTab();
		
	}

	@Override
	public void onValueChange(ValueChangeEvent<Boolean> event) {
		tabpanel.setCloseContextMenu(event.getValue());
	}
	
	private void addTab(){
		assert tabpanel != null : "TabPanel not found !!";
		
		TabItemConfig tabitem = new TabItemConfig();
		tabitem.setText("item " + ++index);
		tabitem.setClosable(true);
		
		tabpanel.add(new HTML("Hello !"+ index), tabitem);
	}

}
